/*
 * Copyright 2018 peipeihh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * limitations under the License.
 */
package com.pphh.demo.remote;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * RemoteSchedulerServer
 *
 * @author huangyinhuang
 * @date 6/5/2018
 */
public class RemoteSchedulerServer {
    private static Logger log = LoggerFactory.getLogger(RemoteSchedulerServer.class);

    public static void main(String[] args) throws SchedulerException {
        RemoteSchedulerServer remoteServer = new RemoteSchedulerServer();
        remoteServer.run();
    }

    public void run() throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();

        log.info("start the scheduler.");
        try {
            scheduler.start();
            // wait one minute to give our jobs a chance to run
            Thread.sleep(60 * 1000L);
        } catch (Exception e) {
            log.info("receive an exception when starting a scheduler.", e.getMessage());
        } finally {
            log.info("shutdown the scheduler gracefully.");
            scheduler.shutdown(true);
        }

        SchedulerMetaData metaData = scheduler.getMetaData();
        log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
    }
}
